package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.CategoryService;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.AddCategoryDTO;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.vo.CategoryQueryVO;
import com.sky.vo.CategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    CategoryMapper categoryMapper;
    /**
     * 新增菜品
     * */
    @Override
    public void addCategory(AddCategoryDTO addCategoryDTO) {
        Category category1 = categoryMapper.selectByName(addCategoryDTO.getName());
        if(category1!=null){
            throw new BaseException("菜品名称不能重复");
        }
        Category category = new Category();
        BeanUtils.copyProperties(addCategoryDTO,category);
        category.setStatus(StatusConstant.ENABLE);
//        category.setCreateUser(BaseContext.getCurrentId());
//        category.setCreateTime(LocalDateTime.now());
//        category.setUpdateTime(LocalDateTime.now());
//        category.setUpdateUser(BaseContext.getCurrentId());
        categoryMapper.insert(category);
    }
    /**
     * 分页查询
     * */

    @Override
    public PageResult<CategoryVO> page1(CategoryPageQueryDTO categoryPageQueryDTO) {
        PageHelper.startPage(categoryPageQueryDTO.getPage(),categoryPageQueryDTO.getPageSize());
                Page<CategoryVO>vo=categoryMapper.page(categoryPageQueryDTO);
                return new PageResult<CategoryVO>(vo.getTotal(),vo.getResult());
    }

    @Override
    public void deleteById(String id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public void update(CategoryDTO categoryDTO) {
       Category category1= categoryMapper.selectByName(categoryDTO.getName());
       if(category1==null){

       }else {
           if(Objects.equals(categoryDTO.getId(),category1.getId())){

           }else {
               throw new BaseException("菜品名称不能重复");
           }

       }

        Category category = new Category();
        BeanUtils.copyProperties(categoryDTO,category);
//        category.setUpdateUser(BaseContext.getCurrentId());
//        category.setUpdateTime(LocalDateTime.now());
         categoryMapper.update(category);




    }

    @Override
    public void jin(Integer status,Long id) {

        Category category = new Category();
        category.setStatus(status);
        category.setId(id);


        categoryMapper.update(category);
    }
   /**
    * 用户端查询分类列表
    * */

    @Override
    public List<Category> list(Integer status,Integer type) {
        return categoryMapper.list(status,type);
    }
}
